{{/* Notify JS that this is a widget page */}}
<span class="js-widget-page d-none"></span>

{{/* Get widget page */}}
{{ $page := "" }}
{{ $headless_bundle := "" }}
{{ if .IsHome }}
  {{ $page = "/home/index.md" }}
  {{ $headless_bundle = site.GetPage $page }}
  {{/* Check homepage exists */}}
  {{ if not $headless_bundle }}
    {{ errorf "Homepage not found at %s!" $page }}
    {{ errorf "Add the `/home/index.md` homepage file to each language's content folder. For example, your site should have a `content/home/` folder containing `index.md` and your homepage sections, or for multi-language sites, `content/en/home/` and `content/zh/home/` etc. Refer to the 'Build Your Homepage' and 'Language' documentation at https://sourcethemes.com/academic/docs/ and the example homepage at https://github.com/gcushen/hugo-academic/tree/master/exampleSite/content/home/index.md ." }}
  {{ end }}
{{ else }}
  {{ $page = .File.Path }}
  {{ $headless_bundle = site.GetPage $page }}
  {{/* Check widget page exists. */}}
  {{ if not $headless_bundle }}
    {{ errorf "Widget Page not found at %s!" $page }}
    {{ errorf "View the Widget Page documentation at https://sourcethemes.com/academic/docs/managing-content/#create-a-widget-page ." }}
    {{ errorf "If the Hugo version is between 0.65 and 0.68, it may be a confirmed Hugo bug that is expected to be fixed in Hugo v0.69: https://github.com/gcushen/hugo-academic/issues/1595#issuecomment-605514973 ." }}
  {{ end }}
{{ end }}

{{/* Load page sections */}}
{{ range $index, $st := where (($headless_bundle.Resources.ByType "page").Match "*.md") ".Params.active" "!=" false }}
  {{/* Begin widget styling */}}
  {{ $bg := $st.Params.design.background }}
  {{ $style := "" }}

  {{ if $bg.color }}
    {{ $style = printf "background-color: %s;" ($bg.color | default "transparent") }}
  {{ end }}

  {{ if and $bg.gradient_start $bg.gradient_end }}
    {{ $style = printf "%sbackground-image: linear-gradient(-45deg, %s, %s);" $style $bg.gradient_start $bg.gradient_end }}
  {{ end }}

  {{/* Fix Hugo`s ContentBaseName returning wrong file base name when page section is within a bundle. */}}
  {{ $path := "" }}
  {{ with .File }}
	  {{ $path = .Path }}
  {{ else }}
	  {{ $path = .Path }}
  {{ end }}
  {{ $hash_id := replace $st.File.ContentBaseName "index" (path.Base (path.Split $path).Dir) }}

  {{ $widget := or $st.Params.widget "blank" }}
  {{ if eq $widget "custom" }}{{ $widget = "blank" }}{{ end }}{{/* Support legacy Custom widget */}}
  {{/* if eq $widget "projects" */}}{{/* $widget = "portfolio" */}}{{/* end */}}{{/* Support legacy Projects widget */}}

  {{ $widget_path := printf "widgets/%s.html" $widget }}
  {{ $widget_args := dict "root" $ "page" $st "hash_id" $hash_id }}
  {{ $css_classes := $st.Params.advanced.css_class | default "" }}
  {{ $extra_attributes := "" }}
  {{ $use_container := true }}

  {{/* Special case: Slider widget. */}}
  {{ if in (slice "slider") $widget }}
    {{ $css_classes = print $css_classes " carousel slide" }}
    {{ $extra_attributes = printf "data-ride=\"carousel\" data-interval=\"%s\"" (string $st.Params.interval | default "5000") }}
    {{ $use_container = false }}
  {{ end }}

  {{ if ne $widget "page_header" }}
    {{ if $bg.image }}
      {{ $darken := "" }}
      {{ if $bg.image_darken }}
        {{ $darken = printf "linear-gradient(rgba(0, 0, 0, %s), rgba(0, 0, 0, %s))," (string $bg.image_darken) (string $bg.image_darken) }}
      {{ end }}
      {{/* See Hugo note on linking assets in styles: https://github.com/gohugoio/hugoThemes#common-permalink-issues */}}
      {{ $style = printf "%sbackground-image: %s url('%s');" $style $darken (printf "img/%s" $bg.image | absURL) }}
      {{ with $bg.image_size }}
        {{/* Allow sizes: actual, cover, and contain. */}}
        {{ $style = printf "%sbackground-size: %s;" $style (replace . "actual" "auto") }}
      {{ end }}
      {{ with $bg.image_position }}
        {{/* Allow valid CSS positions including left, center, and right. */}}
        {{ $style = printf "%sbackground-position: %s;" $style . }}
      {{ end }}
    {{ end }}

    {{ with $st.Params.design.spacing.padding }}
      {{ $style_pad := printf "padding: %s;" (delimit . " ") }}
      {{ $style = print $style $style_pad }}
    {{ end }}
  {{ end }}

  {{ with $st.Params.advanced.css_style }}
    {{ $style = print $style . }}
  {{ end }}

  <section id="{{$hash_id}}" class="home-section {{printf "wg-%s" (replace $widget "_" "-")}} {{if $bg.text_color_light}}dark{{end}} {{if $bg.image}} bg-image{{if ($bg.image_parallax | default true) }} parallax{{end}}{{end}} {{with $css_classes}}{{.}}{{end}}" {{with $style}}style="{{. | safeCSS}}"{{end}} {{print $extra_attributes | safeHTMLAttr}}>
  {{ if eq $widget "page_header" }}
    {{ $page_header_background_css := "" }}
    {{ with $st.Params.design.background.image}}
      {{ $page_header_background_css = printf "img/%s" $st.Params.design.background.image | absURL }}
      {{ $page_header_background_css = printf "background-image: url(%s);" $page_header_background_css }}
    {{ end }}
    {{ with $st.Params.design.background.image_size }}
      {{ $page_header_background_css = printf "%sbackground-size: %s;" $page_header_background_css (replace . "actual" "auto") }}
    {{ end }}
    {{ with $st.Params.design.background.image_position }}
      {{ $page_header_background_css = printf "%sbackground-position: %s;" $page_header_background_css . }}
    {{ end }}
    {{ with $st.Params.design.background.image_opacity }}
      {{ $page_header_background_css = printf "%sopacity: %s;" $page_header_background_css . }}
    {{ end }}
    <div class="page-header-background" style="{{ $page_header_background_css | safeCSS }}"></div>
  {{ end }}
    {{if $use_container}}<div class="container">{{end}}
      {{ partial $widget_path $widget_args }}
    {{if $use_container}}</div>{{end}}
  </section>
{{ end }}
